#!/usr/bin/env bash

# mkdir -p /nat-ddns

fn_download(){
    # github 下载链接
    # wget https://github.com/fatedier/frp/releases/download/v0.34.0/frp_0.34.0_linux_amd64.tar.gz

    # gitee 下载链接
    wget https://gitee.com/buyili/nat-ddns/attach_files/486477/download/frp_0.34.0_linux_amd64.tar.gz
}

# cp frp_0.34.0_linux_amd64.tar.gz /nat-ddns

# cd /nat-ddns
# tar zxvf frp_0.34.0_linux_amd64.tar.gz
# mv frp_0.34.0_linux_amd64 frp


# cd frp

# echo '[common]
# bind_port = 7000
# vhost_http_port = 7001' > ./frps.ini

# echo '
# cd /nat-ddns/frp
# nohup ./frps -c ./frps.ini > frps.log 2>&1 &
# echo "$!" > /run/frps.pid
# ' > ./boot.sh && chmod +x ./boot.sh

# cp ./boot.sh /usr/bin/frps

fn_help() {
    echo ShadowSocksR python client tool
    echo -e if you have not installed ssr, run \`frps install\` first
    echo Usage:
    echo -e "\t" "frps help"
    # echo -e "\n" "Install/Uninstall"
    # echo -e "\t" "frps install      install shadowsocksr client"
    # echo -e "\t" "frps uninstall    uninstall shadowsocksr client"
    # echo -e "\n" "Config and Subscribe"
    # echo -e "\t" "frps update       update subscription from $WEBSITE"
    # echo -e "\t" "frps config       edit config.json"
    # echo -e "\t" "frps xclip        paste configs from clipboard to config.json"
    echo -e "\n" "Start/Stop/Restart"
    echo -e "\t" "frps start        start the shadowsocks service"
    echo -e "\t" "frps stop         stop the shadowsocks service"
    # echo -e "\t" "frps restart      restart the shadowsocks service"
    echo -e "\n" "Testing and Maintenance"
    # echo -e "\t" "frps test         get ip from cip.cc using socks5 proxy"
    echo -e "\t" "frps log          cat the log of shadowsocks"
    # echo -e "\t" "frps shell        cd into frps installation dir"
    echo -e "\t" "frps clean        clean frps configuration backups"
}


# fn_install(){
#     mkdir -p /nat-ddns
#     cd /nat-ddns
#     fn_download
#     tar zxvf frp_0.34.0_linux_amd64.tar.gz
#     mv frp_0.34.0_linux_amd64 frp
#     cd frp

#     read -p "Please input vhost_http_port(7001): " vhost_http_port
#     if [ -z "$vhost_http_port" ];then
#         vhost_http_port="7001"
#     fi

#     echo "[common]
# bind_port = 7000
# vhost_http_port = $vhost_http_port" > ./frps.ini
# }

fn_start(){
    cd /nat-ddns/frp
    nohup ./frps -c ./frps.ini > /var/log/frps.log 2>&1 &
    echo "$!" > /run/frps.pid
}

fn_stop(){
    cd /nat-ddns/frp
    if [ -f /run/frps.pid ];then
        cat /run/frps.pid | xargs kill -9 
    fi
}

fn_log(){
    tail -f /var/log/frps.log
}

fn_clean(){
    fn_stop
    rm -rf /nat-ddns
    rm -rf /var/log/frps.log
    rm -rf /usr/bin/frps
}

fn_main(){
    case $1 in
        -h)           fn_help        ;;
        help)           fn_help        ;;
        # install)        fn_install     ;;
        uninstall)      fn_uninstall   ;;
        update)         fn_update      ;;
        config)         fn_config      ;;
        xclip)          fn_xclip       ;;
        start)          fn_start       ;;
        stop)           fn_stop        ;;
        restart)        fn_restart     ;;
        test)           fn_test        ;;
        log)            fn_log         ;;
        shell)          fn_shell       ;;
        clean)          fn_clean       ;;
        *)              fn_start        ;;
    esac
}

fn_main $1


